package lhc.alg.top100;

import java.util.Random;

/**
 * description: https://leetcode.cn/problems/shuffle-an-array/
 *
 *  洗牌算法
 * author: hongchen.liao
 * date:  2022/8/7
 */
public class _384_Shuffle_an_Array {

    class Solution {
        int[] nums;
        int[] original;

        public Solution(int[] nums) {
            this.nums = nums;
            this.original = new int[nums.length];
            System.arraycopy(nums, 0, original, 0, nums.length);
        }

        public int[] reset() {
            System.arraycopy(original, 0, nums, 0, nums.length);
            return nums;
        }

        public int[] shuffle() {
            Random random = new Random();
            for(int i = 0; i < nums.length; i++){
                int j = i + random.nextInt(nums.length - i);
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
            return nums;
        }
    }

}
